package com.financial.business.yspay.service.exce.impl;

import com.financial.business.yspay.connector.YsBusinessFeign;
import com.financial.business.yspay.constants.FinancialSplitYsEnumConstants;
import com.financial.business.yspay.dao.exce.FinancialSplitExceYsDao;
import com.financial.business.yspay.domain.FinancialSplitYsDO;
import com.financial.business.yspay.service.exce.FinancialYsSplitExceService;
import com.financial.business.yspay.utils.DateUtils;
import com.financial.business.yspay.utils.apiresult.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author WANGNING
 * @Email wyn20140226@aliyun.com
 * @Description //银盛分账异常处理
 * @Date 2019/12/17 15:39
 */
@Service
public class FinancialYsSplitExceServiceImpl implements FinancialYsSplitExceService{
    public static final Logger logger = LoggerFactory.getLogger(FinancialYsSplitExceServiceImpl.class);

    @Autowired
    private FinancialSplitExceYsDao financialSplitExceYsDao;

    @Autowired
    private YsBusinessFeign ysBusinessFeign;
    @Override
    public ApiResult splitExce() {
        /*查询异常订单*/
        Map<String, Object> condMap = new HashMap<>();
        condMap.put("startTime", DateUtils.getTodayZeroTime());
        condMap.put("endTime", DateUtils.getendTime());
        condMap.put("status", FinancialSplitYsEnumConstants.ENUM_STATE_WAIT);
        List splitList = new ArrayList();
        try {
            List<FinancialSplitYsDO> list = financialSplitExceYsDao.list(condMap);
            logger.info("FinancialYsSplitExceServiceImpl.splitExce()---查询到的异常单子是:" + list.size());
            if (list != null && list.size() > 0) {
                for (FinancialSplitYsDO financialSplitYsDO : list) {
                    String returnMessage = financialSplitYsDO.getReturnMessage();
                    String outTradeNo = financialSplitYsDO.getOutTradeNo();
                    logger.info("FinancialYsSplitExceServiceImpl.splitExce()---获取到的异常单子的单号是:" + outTradeNo + "----该笔订单的是分账信息为:" + returnMessage);
                    ApiResult apiResult = this.ysBusinessFeign.ysSplit(returnMessage);
                    /*根据返回信息进行状态判断*/
                    Integer payCode = apiResult.getCode();
                    String apiResultMsg = (String) apiResult.getResult();
                    logger.info("FinancialYsSplitExceServiceImpl.splitExce()---调用银盛分账返回值信息为:" + apiResult);
                    Map<String, Object> map = new HashMap<>();
                    map.put("outTradeNo", outTradeNo);
                    map.put("returnMessage", apiResultMsg);
                    if (payCode == FinancialSplitYsEnumConstants.ENUM_RETURNCODE_SUC && (apiResultMsg != null && apiResultMsg != "")) {
                        /*成功状态*/
                        map.put("state", FinancialSplitYsEnumConstants.ENUM_STATE_SUC);
                        int updateInfoState = financialSplitExceYsDao.updateInfo(condMap);
                        if (updateInfoState > 0) {
                            return ApiResult.success(apiResultMsg);
                        } else {
                            return ApiResult.failure(apiResult);
                        }
                    } else {
                        map.put("state", FinancialSplitYsEnumConstants.ENUM_STATE_FAIL);
                        financialSplitExceYsDao.updateInfo(map);
                        return ApiResult.failure(apiResult);
                    }

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("FinancialYsSplitExceServiceImpl.splitExce()---异常信息:" + e.getMessage());
        }
        if (splitList.size() > 0) {
            return ApiResult.success(splitList);
        } else {
            return ApiResult.success("没有异常的单子");
        }
    }
}
